package com.cnap.multiclouldmgmt.service.impl;

import com.cnap.multiclouldmgmt.MulticlouldmgmtApplicationTests;
import com.cnap.multiclouldmgmt.model.enums.CreateVmStepEnum;
import com.cnap.multiclouldmgmt.model.k8sModels.K8sClusterBasicModel;
import com.cnap.multiclouldmgmt.model.k8sModels.K8sClusterModel;
import com.cnap.multiclouldmgmt.model.vm.VmCreateModel;
import com.cnap.multiclouldmgmt.model.vm.VmModel;
import com.cnap.multiclouldmgmt.repository.master.VmCreateInfoMapper;
import com.cnap.multiclouldmgmt.service.CnapSshService;
import com.cnap.multiclouldmgmt.service.K8sClusterService;
import com.cnap.multiclouldmgmt.service.MultiCloudMgmtService;
import com.cnap.multiclouldmgmt.service.VmResourceService;
import com.cnap.multiclouldmgmt.util.CnapCloudMgmtConstants;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;

import java.util.ArrayList;
import java.util.List;

class VmResourceServiceImplTest extends MulticlouldmgmtApplicationTests {

    @MockBean
    private VmCreateInfoMapper vmCreateInfoMapper;

    @MockBean
    private MultiCloudMgmtService multiCloudMgmtService;

    @MockBean
    private K8sClusterService k8sClusterService;

    @MockBean
    private CnapSshService cnapSshService;

    @Autowired
    private VmResourceService vmResourceService;

    @Test
    void createVmResource() {

        VmCreateModel vmCreateModelExist = new VmCreateModel();
        vmCreateModelExist.setSvcId("svcExist");
        vmCreateModelExist.setType("port");
        Mockito.when(vmCreateInfoMapper.getCreateInfoBySvcId("svcExist")).thenReturn(vmCreateModelExist);
        vmResourceService.createVmResource(vmCreateModelExist);

        VmCreateModel vmCreateModelNew = new VmCreateModel();
        vmCreateModelExist.setSvcId("vmCreateModelNew");
        vmCreateModelNew.setCategory("ai");
        vmCreateModelNew.setVmModels(new ArrayList<>());
        vmResourceService.createVmResource(vmCreateModelNew);

        VmModel vmModel = new VmModel();
        vmCreateModelNew.getVmModels().add(vmModel);
        Mockito.when(vmCreateInfoMapper.getCreateInfoBySvcId("vmCreateModelNew")).thenReturn(vmCreateModelExist);
        vmResourceService.createVmResource(vmCreateModelNew);


    }

    @Test
    void endVmCreateInfo() {
        VmCreateModel vmCreateModelExist = new VmCreateModel();
        vmCreateModelExist.setSvcId("svcExist");
        vmCreateModelExist.setType("port");
        vmResourceService.endVmCreateInfo(vmCreateModelExist);

    }

    @Test
    void handleVmCreateInfo() throws Exception {
        VmCreateModel vmCreateModelNull = new VmCreateModel();
        vmCreateModelNull.setVmModels(new ArrayList<>());
        vmResourceService.handleEmptyVmModels(vmCreateModelNull);

        vmCreateModelNull.setErrorMsg("error");
        vmResourceService.handleVmModels(vmCreateModelNull);

        VmCreateModel vmCreateModel = new VmCreateModel();
        vmCreateModel.setType(CreateVmStepEnum.Init.getDescribe());
        vmCreateModelNull.setType(CreateVmStepEnum.Init.getDescribe());

        vmCreateModel.setSvcId(CnapCloudMgmtConstants.TEST_ADMIN);
        vmCreateModel.setVmModels(new ArrayList<>());
        VmModel vmModel = new VmModel();
        vmCreateModel.getVmModels().add(vmModel);
        vmModel.setBusinessId(CnapCloudMgmtConstants.TEST_ADMIN);
        vmModel.setRegionId("regionId");
        vmModel.setZoneId("zoneId");
        vmModel.setBusinessId(CnapCloudMgmtConstants.TEST_ADMIN);
        vmModel.setUserName(CnapCloudMgmtConstants.TEST_ADMIN);
        vmModel.setCategory("testAi");

        K8sClusterModel k8sClusterModel = new K8sClusterModel();
        k8sClusterModel.setVmModels(new ArrayList<>());
        k8sClusterModel.getVmModels().add(vmModel);
        Mockito.when(multiCloudMgmtService.getK8sClusterModelByZoneId(vmCreateModel))
                .thenReturn(k8sClusterModel);
        Mockito.when(cnapSshService.checkVmNetConnectSuccess(Mockito.any(VmModel.class)))
                .thenReturn(true);
        Mockito.when(multiCloudMgmtService.installK8sFramwork(Mockito.any(VmCreateModel.class)))
                .thenReturn("2.2.2.2");
        K8sClusterBasicModel K8sClusterBasicModelCreate = new K8sClusterBasicModel();
        Mockito.when(k8sClusterService.installCnapScheduleAgent("2.2.2.2",null))
                .thenReturn(K8sClusterBasicModelCreate);
        vmCreateModel.setErrorMsg(null);
        vmResourceService.handleVmModels(vmCreateModel);

    }

    @Test
    void handleVmCreateInfo01() throws Exception {
        VmCreateModel vmCreateModelNull = new VmCreateModel();
        vmCreateModelNull.setVmModels(new ArrayList<>());
        vmResourceService.handleEmptyVmModels(vmCreateModelNull);

        vmCreateModelNull.setErrorMsg("error");
        vmResourceService.handleVmModels(vmCreateModelNull);

        VmCreateModel vmCreateModel = new VmCreateModel();
        vmCreateModel.setType(CreateVmStepEnum.Init.getDescribe());
        vmCreateModelNull.setType(CreateVmStepEnum.Init.getDescribe());

        vmCreateModel.setSvcId(CnapCloudMgmtConstants.TEST_ADMIN);
        vmCreateModel.setVmModels(new ArrayList<>());
        VmModel vmModel = new VmModel();
        vmCreateModel.getVmModels().add(vmModel);
        vmModel.setBusinessId(CnapCloudMgmtConstants.TEST_ADMIN);
        vmModel.setRegionId("regionId");
        vmModel.setZoneId("zoneId");
        vmModel.setBusinessId(CnapCloudMgmtConstants.TEST_ADMIN);
        vmModel.setUserName(CnapCloudMgmtConstants.TEST_ADMIN);
        vmModel.setCategory("testAi");
        vmModel.setVmId("vmId");

        K8sClusterModel k8sClusterModel = new K8sClusterModel();
        k8sClusterModel.setVmModels(new ArrayList<>());
        k8sClusterModel.getVmModels().add(vmModel);
        Mockito.when(multiCloudMgmtService.getK8sClusterModelByZoneId(vmCreateModel))
                .thenReturn(k8sClusterModel);
        Mockito.when(cnapSshService.checkVmNetConnectSuccess(Mockito.any(VmModel.class)))
                .thenReturn(true);
        Mockito.when(multiCloudMgmtService.installK8sFramwork(Mockito.any(VmCreateModel.class)))
                .thenReturn("");
        vmCreateModel.setErrorMsg(null);
        vmResourceService.handleVmModels(vmCreateModel);

    }

    @Test
    void addNewResource() throws Exception {


    }

    @Test
    void testAddNewResource() {
    }

    @Test
    void delVms() {

        List<VmModel> vmModels = new ArrayList<>();
        VmModel vmModel = new VmModel();
        vmResourceService.delVms(vmModels);
    }

    @Test
    void delResource() {

        K8sClusterBasicModel k8sClusterBasicModel = new K8sClusterBasicModel();
        k8sClusterBasicModel.setVmModels(new ArrayList<>());
        VmModel vmModel = new VmModel();
        vmModel.setK8sCluterRole(CnapCloudMgmtConstants.CNAP_VM_WORKER);
        vmModel.setInnerIp("6.6.6.6");
        vmModel.setCategory("ai");
        vmModel.setVmId("vmid");
        k8sClusterBasicModel.getVmModels().add(vmModel);
        VmModel masterModel = new VmModel();
        masterModel.setK8sCluterRole(CnapCloudMgmtConstants.CNAP_VM_MASTER);
        Mockito.when(multiCloudMgmtService.getK8sClusterInfoByName("clustername"))
                .thenReturn(k8sClusterBasicModel);
        vmResourceService.delResource("aliyun","clustername","ai",
                "LILO",1);
    }

    @Test
    void addNewResourceForScale() {

        K8sClusterBasicModel k8sClusterBasicModel = new K8sClusterBasicModel();
        k8sClusterBasicModel.setK8sClusterName(CnapCloudMgmtConstants.TEST_ADMIN);
        k8sClusterBasicModel.setVmModels(new ArrayList<>());
        VmModel vmModel = new VmModel();
        vmModel.setK8sCluterRole(CnapCloudMgmtConstants.CNAP_VM_WORKER);
        vmModel.setInnerIp("6.6.6.6");
        vmModel.setCategory("ai");
        vmModel.setVmId(CnapCloudMgmtConstants.TEST_ADMIN);
        k8sClusterBasicModel.getVmModels().add(vmModel);
        VmModel masterModel = new VmModel();
        masterModel.setK8sCluterRole(CnapCloudMgmtConstants.CNAP_VM_MASTER);
        Mockito.when(multiCloudMgmtService.getK8sClusterInfoByName("clustername"))
                .thenReturn(k8sClusterBasicModel);
        vmResourceService.addNewResourceForScale("aliyun","clustername",
                4,8,"ai");
    }
}